package controlador;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.*;

import modelo.Carrera;


public class ControlCarrera implements KeyListener{
	
	/**
	 * La carrera que controla
	 */
	private Carrera carrera;
	
	private Map<Object,Accion> accionesKeyPressed = new HashMap<Object, Accion>();
	private Map<Object,Accion> accionesKeyReleased = new HashMap<Object, Accion>();

	private boolean habilitado;
	
	public ControlCarrera(Carrera carrera){
		this.carrera = carrera;
		
		//acciones a realizar cuando se presiona una tecla
		accionesKeyPressed.put(KeyEvent.VK_UP, new AccionPisarAcelerador(carrera));
		accionesKeyReleased.put(KeyEvent.VK_UP, new AccionSoltarAcelerador(carrera));
		accionesKeyPressed.put(KeyEvent.VK_DOWN, new AccionPisarFreno(carrera));
		accionesKeyReleased.put(KeyEvent.VK_DOWN, new AccionSoltarFreno(carrera));
	}
	
	@Override
	public void keyPressed(KeyEvent e) {
		if (habilitado && accionesKeyPressed.containsKey(e.getKeyCode())){
			accionesKeyPressed.get(e.getKeyCode()).actuar();
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		if (habilitado && accionesKeyReleased.containsKey(e.getKeyCode())){
			accionesKeyReleased.get(e.getKeyCode()).actuar();
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
	}

	public void habilitar(boolean habilitado) {
		this.habilitado = habilitado;
	}

}
